<template>
  <div>
    <a-tabs :activeKey="first_tab" @change="activeKeyChange">
      <a-tab-pane key="1" tab="未付">
        <a-card :bordered="false" :bodyStyle="tstyle">
          <div class="table-page-search-wrapper">
            <a-form layout="inline">
              <a-row :gutter="48">
                 <a-col :md="6" :sm="24">
                  <a-form-item label="搜索條件">
                    <a-select  style="width: 100%" v-model="queryParam.searchType">
                      <a-select-option  style="width: 100%" :value="0">全部</a-select-option>
                      <a-select-option  style="width: 100%" :value="1">房產名</a-select-option>
                      <a-select-option  style="width: 100%" :value="2">房間名</a-select-option>
                      <a-select-option  style="width: 100%" :value="3">業主姓名</a-select-option>
                      <a-select-option  style="width: 100%" :value="4">業主手機號</a-select-option>
                    </a-select>
                  </a-form-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-item label="賬單類型">
                    <a-select v-model="queryParam.billType"  style="width: 100%" placeholder="请选择賬單類型">
                      <a-select-option v-for="(item,index) in billTypeData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
                    </a-select>
                  </a-form-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-item label="搜索值">
                    <a-input v-model="queryParam.searchValue" allow-clear placeholder="请输入搜索值"/>
                  </a-form-item>
                </a-col>
                <a-col :md="6" :sm="24" >
                  <span class="table-page-search-submitButtons">
                    <a-button type="primary" @click="$refs.table.refresh(true)" >查询</a-button>
                    <a-button style="margin-left: 8px" @click="() => queryParam = { type: 1, numType: 0 }">重置</a-button>
                  </span>
                </a-col>
              </a-row>
            </a-form>
          </div>
        </a-card>
        <a-card :bordered="false">
          <div>
            <template slot="operator">
              <a-row>
                <a-col :span="12">
                  <a-button @click="queryByNumType(0)" :class="queryParam.numType ===0 ? 'numTypeActive' : ''" type="dashed" size="small">全部{{ numTypeData.allNum }}</a-button>
                  <a-button @click="queryByNumType(1)" :class="queryParam.numType ===1 ? 'numTypeActive' : ''" type="dashed" size="small">逾期{{ numTypeData.yqNum }}</a-button>
                  <a-button @click="queryByNumType(2)" :class="queryParam.numType ===2 ? 'numTypeActive' : ''" type="dashed" size="small">今日交租{{ numTypeData.nowNum }}</a-button>
                  <a-button @click="queryByNumType(3)" :class="queryParam.numType ===3 ? 'numTypeActive' : ''" type="dashed" size="small">清算賬單{{ numTypeData.qsNum }}</a-button>
                </a-col>
                <a-col :span="12">
                  <a-button class="flors" @click="exportExcelByType(1)" type="primary">導出賬單</a-button>
                </a-col>
              </a-row>
            </template>
          </div>
          <s-table
            ref="table"
            size="default"
            :columns="columns"
            :data="loadData"
            :alert="true"
            bordered
            :rowKey="(record) => record.id"
            :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }">

            <template class="table-operator" slot="billType"  slot-scope="text" >
              {{ billTypeFilter(text) }}
            </template>
            <template class="table-operator" slot="rentStartDate"  slot-scope="text, record" >
              {{ text }} - {{ record.rentEndDate }}
            </template>
            <template slot="rentAmountName" slot-scope="text,record">
              <a-row v-for="(item,index) in record.details" :key="index" class="aaa">
                <a-col :span="24" class="aa"><span>{{ item.rentAmountName }}</span></a-col>
              </a-row>
            </template>
            <template slot="amount" slot-scope="text,record">
              <a-row v-for="(item,index) in record.details" :key="index" class="aaa">
                <a-col :span="24" :class="item.amount < 0 ? 'abvv': 'ab'" class="aa"><span>{{ item.amount }}</span></a-col>
              </a-row>
            </template>
            <template slot="subtotal" slot-scope="text">
            <span  :class="text < 0 ? 'abvv': 'ab' ">{{ text }}</span>
            </template>
            <template slot="billStatus" slot-scope="text" >
              <span v-if="text === 0">
                <a-tag color="orange" style="font-weight:bold;font-size:15px">
                  {{ statusFilter(text) }}
                </a-tag>
              </span>
              <span v-else-if="text === 1" style="font-weight:bold;font-size:15px">
                <a-tag color="green">
                  {{ statusFilter(text) }}
                </a-tag>
              </span>
              <span  v-else-if="text === 3" style="font-weight:bold;font-size:15px">
                <a-tag color="purple">
                  {{ statusFilter(text) }}
                </a-tag>
              </span>
              <span  v-else>
                <a-tag color="cyan" style="font-weight:bold;font-size:15px">
                  {{ statusFilter(text) }}
                </a-tag>
              </span>
            </template>
            <span slot="action" slot-scope="text, record">
             <a @click="$refs.readDetailForm.edit(record,'','',2)">查看</a>
             <a-divider type="vertical" />
              <a-dropdown >
                <a class="ant-dropdown-link">
                  更多 <a-icon type="down" />
                </a>
                <a-menu slot="overlay">
                  <a-menu-item v-if="record.billStatus ===0">
                    <a @click="$refs.arriveForm.edit(record)">到賬</a>
                  </a-menu-item>
                  <a-menu-item v-if="record.hyCb ===1 && (record.billStatus ===0 || record.billStatus ===3)">
                    <a @click="$refs.addFormMeterOne.add(record)">抄表</a>
                  </a-menu-item>
                  <a-menu-item v-if="record.billStatus ===0 || record.billStatus ===3">
                    <a @click="$refs.editFormOne.edit(record,'','',1)">修改</a>
                  </a-menu-item>
                  <a-menu-item v-if="record.billStatus ===0">
                    <a @click="$refs.tuoFormOne.edit(record)">拖欠</a>
                  </a-menu-item>
                  <a-menu-item v-if="record.billStatus ===3">
                    <a @click="$refs.tzqsForm.edit(record.id)">退租清算</a>
                  </a-menu-item>
                  <a-menu-item>
                    <a @click="$refs.printBillDetail.edit(record.id)">打印</a>
                  </a-menu-item>
                  <a-menu-item>
                    <a @click="$refs.leaseDetail.edit(record.houseLeaseId)">租約詳情</a>
                  </a-menu-item>
                </a-menu>
              </a-dropdown>
            </span>
          </s-table>
        </a-card>
      </a-tab-pane>
      <a-tab-pane key="2" tab="已付">
        <a-card :bordered="false" :bodyStyle="tstyle">
          <div class="table-page-search-wrapper">
            <a-form layout="inline">
              <a-row :gutter="48">
                <a-col :md="6" :sm="24">
                  <a-form-item label="搜索條件">
                    <a-select  style="width: 100%" v-model="queryParam2.searchType">
                      <a-select-option  style="width: 100%" :value="0">全部</a-select-option>
                      <a-select-option  style="width: 100%" :value="1">房產名</a-select-option>
                      <a-select-option  style="width: 100%" :value="2">房間名</a-select-option>
                      <a-select-option  style="width: 100%" :value="3">業主姓名</a-select-option>
                      <a-select-option  style="width: 100%" :value="4">業主手機號</a-select-option>
                    </a-select>
                  </a-form-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-item label="賬單類型">
                    <a-select v-model="queryParam2.billType"  style="width: 100%" placeholder="请选择賬單類型">
                      <a-select-option v-for="(item,index) in billTypeData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
                    </a-select>
                  </a-form-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-item label="搜索值">
                    <a-input v-model="queryParam2.searchValue" allow-clear placeholder="请输入搜索值"/>
                  </a-form-item>
                </a-col>
                <a-col :md="6" :sm="24" >
                  <span class="table-page-search-submitButtons">
                    <a-button type="primary" @click="$refs.table2.refresh(true)" >查询</a-button>
                    <a-button style="margin-left: 8px" @click="() => queryParam2 = { type: 2 }">重置</a-button>
                  </span>
                </a-col>
              </a-row>
            </a-form>
          </div>
        </a-card>
        <a-card :bordered="false">
          <div>
              <a-button type="primary" @click="exportExcelByType(2)">導出賬單</a-button>
          </div>
          <s-table
            ref="table2"
            size="default"
            :columns="columns"
            :data="loadData2"
            :alert="true"
            bordered
            :rowKey="(record) => record.id"
            :rowSelection="{ selectedRowKeys: selectedRowKeys2, onChange: onSelectChange2 }"
          >
            <template class="table-operator" slot="billType"  slot-scope="text" >
              {{ billTypeFilter(text) }}
            </template>
            <template class="table-operator" slot="rentStartDate"  slot-scope="text, record" >
              {{ text }} - {{ record.rentEndDate }}
            </template>
             <template slot="rentAmountName" slot-scope="text,record">
              <a-row v-for="(item,index) in record.details" :key="index" class="aaa">
                <a-col :span="24" class="aa"><span>{{ item.rentAmountName }}</span></a-col>
              </a-row>
            </template>
            <template slot="amount" slot-scope="text,record">
              <a-row v-for="(item,index) in record.details" :key="index" class="aaa">
                <a-col :span="24" :class="item.amount < 0 ? 'abvv': 'ab'" class="aa"><span>{{ item.amount }}</span></a-col>
              </a-row>
            </template>
            <template slot="subtotal" slot-scope="text">
            <span  :class="text < 0 ? 'abvv': 'ab' ">{{ text }}</span>
            </template>
            <template slot="billStatus" slot-scope="text" >
              <span v-if="text === 0">
                <a-tag color="orange" style="font-weight:bold;font-size:15px">
                  {{ statusFilter(text) }}
                </a-tag>
              </span>
              <span v-else-if="text === 1">
                <a-tag color="green" style="font-weight:bold;font-size:15px">
                  {{ statusFilter(text) }}
                </a-tag>
              </span>
              <span  v-else-if="text === 3">
                <a-tag color="purple" style="font-weight:bold;font-size:15px">
                  {{ statusFilter(text) }}
                </a-tag>
              </span>
              <span  v-else>
                <a-tag color="cyan" style="font-weight:bold;font-size:15px">
                  {{ statusFilter(text) }}
                </a-tag>
              </span>
            </template>
            <span slot="action" slot-scope="text, record">
              <a @click="$refs.readDetailForm.edit(record,'','',2)">查看</a>
             <a-divider type="vertical" />
              <a-dropdown >
                <a class="ant-dropdown-link">
                  更多 <a-icon type="down"/>
                </a>
                <a-menu slot="overlay">
                  <a-menu-item>
                    <a-popconfirm  placement="topRight" title="撤銷到賬將刪除此賬單到賬時產生的流水？" @confirm="() => chexiaoDaoZhang(record.id)">
                      <a>撤銷到賬</a>
                    </a-popconfirm>
                  </a-menu-item>
                  <a-menu-item>
                    <a @click="$refs.printBillDetail.edit(record.id)">打印</a>
                  </a-menu-item>
                  <a-menu-item>
                    <a @click="$refs.leaseDetail.edit(record.houseLeaseId)">租約詳情</a>
                  </a-menu-item>
                </a-menu>
              </a-dropdown>
            </span>
          </s-table>
        </a-card>
      </a-tab-pane>
    </a-tabs>
    <arrive-form ref="arriveForm" @ok="handleOk" />
    <add-form-meter-one ref="addFormMeterOne" @ok="handleOk" />
    <edit-form-one ref="editFormOne" @ok="handleOk" />
    <tuo-form-one ref="tuoFormOne" @ok="handleOk" />
    <tzqs-form ref="tzqsForm" @ok="handleOk" />
    <print-bill-detail ref="printBillDetail"/>
    <lease-detail ref="leaseDetail" @ok="handleOk" />
    <read-detail-form ref="readDetailForm"/>
  </div>
</template>
<script>
  import { STable } from '@/components'
  import moment from 'moment'
  import { houseBillPage, houseBillDelete, houseBillcheXiaoDaoZhangEdit, getHouseBillNum, exportExcel } from '@/api/modular/main/housebill/houseBillManage'
  import arriveForm from './arriveForm.vue'
  import addFormMeterOne from '../hydropowermeter/addFormMeterOne.vue'
  import editFormOne from './editFormOne.vue'
  import tuoFormOne from './tuoFormOne.vue'
  import tzqsForm from './tzqsForm.vue'
  import printBillDetail from './printBillDetail.vue'
  import leaseDetail from '../houselease/leaseDetail.vue'
  import readDetailForm from './readDetailForm.vue'
  export default {
    components: {
      STable,
      arriveForm,
      addFormMeterOne,
      editFormOne,
      tuoFormOne,
      tzqsForm,
      printBillDetail,
      leaseDetail,
      readDetailForm
    },
    data () {
      return {
        // 高级搜索 展开/关闭
        advanced: false,
        first_tab: '1',
        // 查询参数
        queryParam: { type: 1, numType: 0 },
        queryParam2: { type: 2 },
        // 表头
        columns: [
          {
            title: '房間',
            align: 'center',
            dataIndex: 'roomName'
          },
          {
            title: '承租人',
            align: 'center',
            dataIndex: 'name'
          },
          {
            title: '收租日',
            align: 'center',
            dataIndex: 'rentedDay'
          },
          {
            title: '賬單週期',
            align: 'center',
            dataIndex: 'rentStartDate',
            scopedSlots: { customRender: 'rentStartDate' }
          },
          {
            title: '類別',
            align: 'center',
            dataIndex: 'rentAmountName',
            scopedSlots: { customRender: 'rentAmountName' }
          },
          {
            title: '金额',
            align: 'center',
            dataIndex: 'amount',
            scopedSlots: { customRender: 'amount' }
          },
          {
            title: '小計',
            align: 'center',
            dataIndex: 'subtotal',
            scopedSlots: { customRender: 'subtotal' }
          },
          {
            title: '狀態',
            align: 'center',
            dataIndex: 'billStatus',
            scopedSlots: { customRender: 'billStatus' }
          },
          {
            title: '操作',
            width: '150px',
            dataIndex: 'action',
            scopedSlots: { customRender: 'action' }
          }
        ],
        tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          this.initData()
          return houseBillPage(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        },
        loadData2: parameter => {
          return houseBillPage(Object.assign(parameter, this.queryParam2)).then((res) => {
            return res.data
          })
        },
        selectedRowKeys: [],
        selectedRowKeys2: [],
        selectedRows: [],
        selectedRows2: [],
        billTypeData: [],
        billStatusDictTypeDropDown: [],
        numTypeData: {}
      }
    },
    created () {
      const sourceOption = this.$options
      this.billTypeData = sourceOption.filters['dictData']('bill_type')
      this.billStatusDictTypeDropDown = sourceOption.filters['dictData']('bill_status')
    },
    methods: {
      moment,
      initData() {
         getHouseBillNum(this.queryParam).then((res) => {
          if (res.code === 200) {
            this.numTypeData = res.data
          } else {
            this.$message.error(res.msg)
          }
        })
      },
      exportExcelByType(type) {
        if (type === 1) {
           this.queryParam.excelType = type
          exportExcel(this.queryParam).then((res) => {
             this.downloadfile(res)
          })
        } else {
           this.queryParam2.excelType = type
          exportExcel(this.queryParam2).then((res) => {
             this.downloadfile(res)
          })
        }
      },
      downloadfile (res) {
        var blob = new Blob([res.data], { type: 'application/octet-stream;charset=UTF-8' })
        var contentDisposition = res.headers['content-disposition']
        var patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*')
        var result = patt.exec(contentDisposition)
        var filename = result[1]
        var downloadElement = document.createElement('a')
        var href = window.URL.createObjectURL(blob) // 创建下载的链接
        var reg = /^["](.*)["]$/g
        downloadElement.style.display = 'none'
        downloadElement.href = href
        downloadElement.download = decodeURI(filename.replace(reg, '$1')) // 下载后文件名
        document.body.appendChild(downloadElement)
        downloadElement.click() // 点击下载
        document.body.removeChild(downloadElement) // 下载完成移除元素
        window.URL.revokeObjectURL(href)
      },
      queryByNumType(type) {
        this.queryParam.numType = type
        this.$refs.table.refresh(true)
      },
       // 第一步tab点击
      activeKeyChange(key) {
        this.first_tab = key
      },
      billTypeFilter (type) {
        const values = this.billTypeData.filter(item => item.code === type + '')
        if (values.length > 0) {
          return values[0].name
        }
      },
      statusFilter (sex) {
        // eslint-disable-next-line eqeqeq
        const values = this.billStatusDictTypeDropDown.filter(item => item.code == sex)
        if (values.length > 0) {
          return values[0].name
        }
      },
      houseBillDelete (record) {
        houseBillDelete(record).then((res) => {
          if (res.code === 200) {
            this.$message.success('删除成功')
            this.$refs.table.refresh()
          } else {
            this.$message.error('删除失败') // + res.message
          }
        })
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      onChangerentStartDate(date, dateString) {
        this.rentStartDateDateString = dateString
      },
      onChangerentEndDate(date, dateString) {
        this.rentEndDateDateString = dateString
      },
      handleOk () {
        this.$refs.table.refresh()
        this.$refs.table2.refresh()
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      },
      onSelectChange2 (selectedRowKeys, selectedRows) {
        this.selectedRowKeys2 = selectedRowKeys
        this.selectedRows2 = selectedRows
      },
      chexiaoDaoZhang(id) {
        houseBillcheXiaoDaoZhangEdit({ id: id }).then((res) => {
          if (res.code === 200) {
            this.handleOk()
          } else {
            this.$message.error(res.msg)//  + res.message
          }
        })
      }
    }
  }
</script>
<style lang="less" scoped>
  .table-operator {
    margin-bottom: 18px;
  }
  button {
    margin-right: 8px;
  }
  .aaa{
    border-bottom: 1px solid #ccc;
    margin-bottom: 6px;
  }
  .aa{
    text-align: center;
    font-size: 16px;
  }
  .ab{
    color: red;
  }
  .abvv{
    color: green;
  }
  .numTypeActive{
    color: #40a9ff;
    background-color: #fff;
    border-color: #40a9ff;
  }
  .flors{
    float: right;
  }
</style>
